//
//  MainPageTableview.m
//  diaoyuren
//
//  Created by 钓鱼人 on 16/2/16.
//  Copyright © 2016年 game. All rights reserved.
//

#import "MainPageTableview.h"
#import "FunctionCell.h"
#import "SelectionCell.h"
#import "ImageCycly.h"
#import "FunSelectController.h"
#import "ForumClickController.h"
#import "Pagecarousel_listModel.h"
#import "Manager.h"
@interface MainPageTableview ()<UITableViewDataSource,UITableViewDelegate,SelectionCellDeleget,FunctionCellSelectDeleget,WMLoopViewDelegate>

@property (nonatomic,assign) CGFloat FunCellHight;
@property (nonatomic,assign) BOOL isZan;
@property (nonatomic,assign) CGFloat selectCellHight;
@property (nonatomic, strong) NSMutableDictionary * cellHeightdic;

/** mae*/
@property ( nonatomic, copy) NSString *made;

@end

@implementation MainPageTableview


- (NSMutableDictionary *)cellHeightdic{

    

    if (!_cellHeightdic) {
        
        _cellHeightdic = [NSMutableDictionary dictionary];
    }
    return _cellHeightdic;
}

-(instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style{
    if (self = [super initWithFrame:frame style:style]) {
        self.dataSource = self;
        self.delegate = self;
        
        self.backgroundColor = tabGropBackColor;
        
        //注册cell
        [self registerClass:[FunctionCell class] forCellReuseIdentifier:@"functioncell"];
        [self registerNib:[UINib nibWithNibName:@"SelectionCell" bundle:nil] forCellReuseIdentifier:@"selectcell"];
        
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width, 0.1)];
        view.backgroundColor = [UIColor clearColor];
        self.tableFooterView = view;

        self.sectionFooterHeight = 0.1f;
        self.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
        return self;
}

-(void)setMainPageModel:(MainPageModel *)mainPageModel{
    _mainPageModel = mainPageModel;
     self.tableHeaderView = [self ImageCyclyView];
}

#pragma mark--轮播图
-(UIView *)ImageCyclyView{
    
    NSMutableArray *imgCycleAry = [NSMutableArray array];
    
    for (Pagecarousel_listModel *imgModel in self.mainPageModel.carousel_list) {
        [imgCycleAry addObject:imgModel.img];
    }
    
    ImageCycly *imgcycly = [[ImageCycly alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT*0.16) images:imgCycleAry autoPlay:YES delay:8 titleAray:nil];
    
    imgcycly.delegate = self;
    return imgcycly;
}

/**
 *  轮播图的点击代理
 */
-(void)loopViewDidSelectedImage:(ImgUIview *)loopView index:(int)index{
    Pagecarousel_listModel *imgModel = self.mainPageModel.carousel_list[index];
    
    if( imgModel.tid ==0){
        imgModel.tid = [NSString stringWithFormat:@"%d",6857];
    };
    
    NSString *url = [NSString stringWithFormat:MainSelet,imgModel.tid];
    [self.Maindeleget MainPageTableviewSelect:imgModel.tid Url:url Slide:NO Row:100000];

}

#pragma mark---UITableViewDeleget
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}


//cell个数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section==0) {
         return  1;
    }else{
        return self.mainPageModel.thread_list.count;
    }
}


//每行cell的高度，根据内容动态显示
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section==0) {
        return self.FunCellHight+10;
    }else{
        return [[self.cellHeightdic valueForKey:[NSString stringWithFormat:@"%ld-%ld",(long)indexPath.section, (long)indexPath.row]] floatValue]+1;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section==1) {
        
        if (self.mainPageModel.thread_list.count==0) {
            return 0;
        }else
            return 40;
    }else{
            return 0;
    }
}
#pragma mark--UITableViewDatasouce
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (self.mainPageModel.thread_list.count==0) {
        return nil;
    }else
        if (section==1) {
        UIView *backview = [[UIView alloc]init];
        backview.backgroundColor =  RGBA(235, 235, 241, 1);//RGBA(245, 245, 245, 1);
        
        UIView *whiteView = [[UIView alloc]initWithFrame:CGRectMake(0, 10, SCREEN_WIDTH, 30)];
        whiteView.backgroundColor  = [UIColor whiteColor];
        [backview addSubview:whiteView];
        
        UIImageView  *imageview = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH/3.5, 30)];
            [imageview setImage:[UIImage imageNamed:@"jingxuan"]];
        [whiteView addSubview:imageview];
        
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, W(imageview)-20, H(imageview))];
        label.textColor = [UIColor whiteColor];
        label.text = @"每日精选";
        label.textAlignment = NSTextAlignmentCenter;
        label.font = font(SCREEN_WIDTH*0.043);
        [imageview addSubview:label];
        
        return backview;
        
    }else{
        return nil;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSString *key = [NSString stringWithFormat:@"%ld-%ld",(long)indexPath.section, (long)indexPath.row];
    if (indexPath.section==0) {
        
        FunctionCell *cell = [tableView dequeueReusableCellWithIdentifier:@"functioncell"];
        cell.deleget = self;
        self.FunCellHight = cell.CellHight-10;
        cell.indexModel = self.pagesModel.index;

        return cell;
        
    }else{

        SelectionCell *cell =[tableView dequeueReusableCellWithIdentifier:@"selectcell"];

        cell.section = indexPath.row;
        cell.deleget= self;
        cell.formHash = self.mainPageModel.formhash;
        
         cell.model = self.mainPageModel.thread_list[indexPath.row];
        
        [self.cellHeightdic setValue:[NSString stringWithFormat:@"%f",cell.height] forKey:key];
       
        return cell;
    }
}

/**
 *  Cell中 评论点击代理
 */
-(void)CommentSelect:(NSString *)tid Row:(NSInteger)row{
    
    NSString *url = [NSString stringWithFormat:MainSelet,tid];
    
    [self.Maindeleget MainPageTableviewSelect:tid Url:url Slide:YES Row:row];
}

/**
 * Cell上 点赞代理方法
 */
-(void)PraimSelect:(NSInteger)section {
    
    thread_listModel *model = self.mainPageModel.thread_list[section];
    model.isZan = YES;
}

-(void)PramisNoLogin{
    [self.Maindeleget PramisNoLogin];
}

-(void)PraimFile:(NSString *)message{
    [Manager GetHudWhithText:message AndFatherView:self];
    
}

-(void)SelectPrais:(NSInteger)section{
 
    thread_listModel *model = self.mainPageModel.thread_list[section];
    model.isZan = YES;
}

-(void)UserSpaceClick:(NSString *)uid{

    [self.Maindeleget UserSpaceClick:uid];
}

/**
 *  功能模块点击事件
 */
-(void)FunctionCellSelect:(NSInteger)item{
   
    AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
    if (item==2) {
        app.myTabBar.selectedIndex = 2;
    }
    
        [self.Maindeleget FunctionCellSelect:item];
    
    }

/**
 *  论坛Cell点击事件
 */
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    SelectionCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    NSString *url = [NSString stringWithFormat:MainSelet,cell.tid];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [self.Maindeleget MainPageTableviewSelect:cell.tid Url:url Slide:NO Row:indexPath.row];

    });
}


@end
